<?php

class SettingController extends AdminController
{    
    public function actionUpdate($id)
    {
        $model = $this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['Setting'])) {
            $model->attributes = $_POST['Setting'];
            if ($model->save())
                $this->redirect(array('view', 'id' => $model->id));
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    /**
     * Manages all models.
     */
    public function actionIndex()
    {
        $model = new SettingForm();
        $model->scenario = "updateSettings";
        $setting = Yii::app()->setting;
        if (isset($_POST['SettingForm'])) {
            
            $model->attributes = $_POST['SettingForm'];
//            var_dump($_POST);die();
//            var_dump($model);die();
            if ($model->validate()) {
                $setting->setDbItem('transportType', $model->transportType);
                foreach(SettingForm::$arrSmtp as $key => $value)
                {
                    $setting->setDbItem($value, $model->$value);
                }

                foreach(SettingForm::$arrSmtp1 as $key => $value)
                {
                    $setting->setDbItem($value, $model->$value);
                }
//                var_dump($setting);die();
                foreach(SettingForm::$arrGeneral as $key => $value)
                {
                    if($value != 'image_watermark')
                    $setting->setDbItem($value, $model->$value);
                }

                $setting->setDbItem('link_youtube', $model->link_youtube);
                //$setting->setDbItem('comment_facebook', $model->comment_facebook);
                
                $filelogo = CUploadedFile::getInstance($model,'logo');
                /*if($filelogo !== null){
                    $baseImagePath = YII_UPLOAD_DIR . '/logo/';
                    $newName = 'logo' . '_' . time() . rand(1,10000). '.' . $filelogo->extensionName;
                    Yii::log($newName, 'error');
                    if($filelogo->saveAs($baseImagePath.$newName))
                    {
                        $model->logo = $newName;
                        $setting->setDbItem('logo', $newName);
                        
  //                           //resize image logo
                               $ImageProcessing = new ImageProcessing();       
                               $ImageProcessing->folder = '/data/logo/';
                               $ImageProcessing->file = $newName;
                               $ImageProcessing->thumbs =array(
                                                                'small'=>array('width'=>190,'height'=>55),
//                                                                'logo_footer'=>array('width'=>183,'height'=>34),
                                                              );  
                               $ImageProcessing->create_thumbs();
                    }
                } else{
                    $model->logo = $setting->getItem('logo');
                    $setting->setDbItem('logo',Yii::app()->params['logo']);
                }  
                */
               // $fileslogan = CUploadedFile::getInstance($model,'slogan_vi');
               /* if($fileslogan !== null){
                    $baseImagePath = YII_UPLOAD_DIR . '/logo/';
                    $newName = 'slogan_vi' . '_' . time() . rand(1,10000). '.' . $fileslogan->extensionName;
                    Yii::log($newName, 'error');
                    if($fileslogan->saveAs($baseImagePath.$newName))
                    {
                        $model->logo = $newName;
                        $setting->setDbItem('slogan_vi', $newName);
                        
  //                           //resize image logo
                               $ImageProcessing = new ImageProcessing();       
                               $ImageProcessing->folder = '/data/logo/';
                               $ImageProcessing->file = $newName;
                               $ImageProcessing->thumbs =array(
                                                                'small'=>array('width'=>303,'height'=>44),
//                                                                'logo_footer'=>array('width'=>183,'height'=>34),
                                                              );  
                               $ImageProcessing->create_thumbs();
                    }
                } else{
                    $model->logo = $setting->getItem('slogan_vi');
                    $setting->setDbItem('slogan_vi',Yii::app()->params['slogan_vi']);
                }  */
                /*$fileslogan_en = CUploadedFile::getInstance($model,'slogan_en');
                if($fileslogan_en !== null){
                    $baseImagePath = YII_UPLOAD_DIR . '/logo/';
                    $newName = 'slogan_en' . '_' . time() . rand(1,10000). '.' . $fileslogan_en->extensionName;
                    Yii::log($newName, 'error');
                    if($fileslogan_en->saveAs($baseImagePath.$newName))
                    {
                        $model->logo = $newName;
                        $setting->setDbItem('slogan_en', $newName);
                        
  //                           //resize image logo
                               $ImageProcessing = new ImageProcessing();       
                               $ImageProcessing->folder = '/data/logo/';
                               $ImageProcessing->file = $newName;
                               $ImageProcessing->thumbs =array(
                                                                'small'=>array('width'=>303,'height'=>44),
//                                                                'logo_footer'=>array('width'=>183,'height'=>34),
                                                              );  
                               $ImageProcessing->create_thumbs();
                    }
                } else{
                    $model->logo = $setting->getItem('slogan_en');
                    $setting->setDbItem('slogan_en',Yii::app()->params['slogan_en']);
                }
                $filebackground = CUploadedFile::getInstance($model,'background');
                if($filebackground !== null){
                    $baseImagePath = YII_UPLOAD_DIR . '/background/';
                    $newNameBackGround = 'background' . '_' . time() . rand(1,10000). '.' . $filebackground->extensionName;
                    Yii::log($newNameBackGround, 'error');
                    if($filebackground->saveAs($baseImagePath.$newNameBackGround))
                    {
                        $model->background = $newNameBackGround;
                        $setting->setDbItem('background', $newNameBackGround);
                        
  //                           //resize image logo
                               $ImageProcessing = new ImageProcessing();       
                               $ImageProcessing->folder = '/data/background/';
                               $ImageProcessing->file = $newNameBackGround;
                               $ImageProcessing->thumbs =array(
                                                                'small'=>array('width'=>190,'height'=>55),
//                                                                'logo_footer'=>array('width'=>183,'height'=>34),
                                                              );  
                               $ImageProcessing->create_thumbs();
                    }
                } else{
                    $model->background = $setting->getItem('background');
                    $setting->setDbItem('background',Yii::app()->params['background']);
                }                  
                
                
                */
                Yii::app()->user->setFlash('setting', 'Cài đặt đã được cập nhật.');
                $this->refresh();

            }
        } else {
            $temp = $setting->getItem('transportType');

            if (!empty($temp)) {
                $model->transportType = $setting->getItem('transportType');
//                $model->transportType1 = $setting->getItem('transportType1');
            } else if(!empty(Yii::app()->mail->transportType)) {
                $model->transportType = Yii::app()->mail->transportType;
//                $model->transportType1 = Yii::app()->mail->transportType1;
            }

            foreach(SettingForm::$arrSmtp as $key => $value)
            {                
                $temp = $setting->getItem($value);
                if (!empty($temp)) {
                    $model->$value = $setting->getItem($value);
                } else if(!empty(Yii::app()->mail->transportOptions[$key])) {
                    $model->$value = Yii::app()->mail->transportOptions[$key];
                }
            }
            foreach(SettingForm::$arrSmtp1 as $key => $value)
            {
                $temp = $setting->getItem($value);
                if (!empty($temp)) {
                    $model->$value = $setting->getItem($value);
                } else if(!empty(Yii::app()->mail->transportOptions[$key])) {
                    $model->$value = Yii::app()->mail->transportOptions[$key];
                }
            }
//            var_dump($model);die();
            foreach(SettingForm::$arrGeneral as $key => $value)
            {                
                $temp = $setting->getItem($value);
                if (!empty($temp)) {
                    $model->$value = $setting->getItem($value);
                } else if(!empty(Yii::app()->mail->transportOptions[$key])) {
                    $model->$value = Yii::app()->mail->transportOptions[$key];
                }
            }
        }
        $this->render('index', array('model' => $model, ));
    }

    /**
     * Returns the data model based on the primary key given in the GET variable.
     * If the data model is not found, an HTTP exception will be raised.
     * @param integer the ID of the model to be loaded
     */
    public function loadModel($id)
    {
        $model = Setting::model()->findByPk($id);
        if ($model === null)
        {
            Yii::log('The requested page does not exist.');
            throw new CHttpException(404,'The requested page does not exist.');
        }
        return $model;
    }

    /**
     * Performs the AJAX validation.
     * @param CModel the model to be validated
     */
    protected function performAjaxValidation($model)
    {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'setting-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }
}
